# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Ray Donnelly <mingw.android@gmail.com>

_realname=blender
pkgbase=mingw-w64-${_realname}-git
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}-git"
provides=("${MINGW_PACKAGE_PREFIX}-${_realname}")
conflicts=("${MINGW_PACKAGE_PREFIX}-${_realname}")
blender_version=2.74
pkgver=r58316.c32ded3
_pyver=3.5
pkgrel=1
pkgdesc="A fully integrated 3D graphics creation suite (mingw-w64)"
arch=('any')
license=('GPL')
url="https://www.blender.org/"
depends=("${MINGW_PACKAGE_PREFIX}-boost"
         "${MINGW_PACKAGE_PREFIX}-glew"
         "${MINGW_PACKAGE_PREFIX}-ffmpeg"
         "${MINGW_PACKAGE_PREFIX}-fftw"
         "${MINGW_PACKAGE_PREFIX}-freetype"
         "${MINGW_PACKAGE_PREFIX}-libpng"
         "${MINGW_PACKAGE_PREFIX}-libsndfile"
         "${MINGW_PACKAGE_PREFIX}-libtiff"
         "${MINGW_PACKAGE_PREFIX}-openexr"
         "${MINGW_PACKAGE_PREFIX}-openal"
         "${MINGW_PACKAGE_PREFIX}-opencollada"
         "${MINGW_PACKAGE_PREFIX}-opencolorio"
         "${MINGW_PACKAGE_PREFIX}-openimageio"
         "${MINGW_PACKAGE_PREFIX}-openshadinglanguage-git"
         "${MINGW_PACKAGE_PREFIX}-python3"
         "${MINGW_PACKAGE_PREFIX}-python3-numpy"
         "${MINGW_PACKAGE_PREFIX}-wintab-sdk")
makedepends=("${MINGW_PACKAGE_PREFIX}-clang"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-gcc"
             "git")
#[[ $CARCH == x86_64 ]] && makedepends+=('cuda')
#optdepends=('cuda: cycles renderer cuda support')
options=(!strip staticlibs !buildflags)
#install=blender.install
source=("${_realname}"::"git://git.blender.org/blender.git"
        "translations"::"git://git.blender.org/blender-translations.git"
        "addons"::"git://git.blender.org/blender-addons.git"
        "addons-contrib"::"git://git.blender.org/blender-addons-contrib.git"
        "mingw-w64-cmake.patch"
        "fix-mingw-w64-definitions.patch"
        "mingw-dont-install-3rdparty.patch"
        "fix-finding-glew.patch"
        "fix-define-syntax.patch"
        "mingw-DEBUG-libmv-avoid-too-many-sections.patch"
        "mingw-DEBUG-dont-suffix-libs-with-_d.patch"
        "mingw-use-X11-like-path-layout.patch"
        "osl-deprecate-api.patch")
sha256sums=('SKIP'
            'SKIP'
            'SKIP'
            'SKIP'
            'dca840550978d23fd12e34393a63c46df4d7e2ec2c894cbc5bccf3832c16839b'
            '1b33debd05f5378fa5010dbf341dca802b461f2b3cd19cb8bc204b5b87f60ca1'
            '3ff7bf7ea57faaa04c5ddab28ec597ceea355537b1acfbe46cccf66487b70cbb'
            '6c04651a6d49478f10a1be7874a455614ce74987543f6d91624c3a28c1debaf8'
            '36a400c0f56f0f1ad0b6c7f9fad96c89fdc72dc0002af22f7f7fdbddafcdaddc'
            'd3f4912e401b861af5bc66753098d1987892554973976bc6186c3ae49d8c43cb'
            '02dac9a97126f67250b80065800b24fc7fe3ed5f6cf9f36f41e3535d9e8f64d4'
            '31cb3c2fb177f774a0f401002be31e10ddf705cfd3dd35f0d778c34ee9e389d5'
            '0d35523c84db1b1fd53c474d132f90e24f6e7106ed12cf721216be529f144d8b')

pkgver() {
  cd "$srcdir/$_realname"
  printf "r%s.%s" "$(git rev-list --count HEAD)" "$(git rev-parse --short HEAD)"
}

prepare() {
  cd "$srcdir/$_realname"
  patch -p1 -i "${srcdir}"/mingw-w64-cmake.patch
  patch -p1 -i "${srcdir}"/fix-mingw-w64-definitions.patch
  patch -p1 -i "${srcdir}"/mingw-dont-install-3rdparty.patch
  patch -p1 -i "${srcdir}"/fix-finding-glew.patch
  patch -p1 -i "${srcdir}"/fix-define-syntax.patch
  patch -p1 -i "${srcdir}"/mingw-DEBUG-libmv-avoid-too-many-sections.patch
  patch -p1 -i "${srcdir}"/mingw-DEBUG-dont-suffix-libs-with-_d.patch
  patch -p1 -i "${srcdir}"/mingw-use-X11-like-path-layout.patch
  patch -p1 -i "${srcdir}"/osl-deprecate-api.patch

  cp -rf $srcdir/translations/* $srcdir/$_realname/release/datafiles/locale/
  cp -rf $srcdir/addons/* $srcdir/$_realname/release/scripts/addons/
  cp -rf $srcdir/addons-contrib/* $srcdir/$_realname/release/scripts/addons_contrib/
}

build() {
  mkdir $srcdir/build-${MINGW_CHOST}
  cd $srcdir/build-${MINGW_CHOST}

  [[ $CARCH == i686 ]] && ENABLESSE2="-DSUPPORT_SSE2_BUILD=OFF"
  [[ $CARCH == i686 ]] && BUILDCUDA="OFF" || BUILDCUDA="ON"
  #[[ $CARCH == i686 ]] && PMARCH="i686" || PMARCH="x86-64"

  declare -a _btype
  if check_option "debug" "y"; then
    _btype=Debug
  else
    _btype=Release
  fi

  ${MINGW_PREFIX}/bin/cmake \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX=${pkgdir}${MINGW_PREFIX} \
    -DMINGW_LIBS=${MINGW_PREFIX} \
    -DCMAKE_BUILD_TYPE=${_btype} \
    -DWITH_SYSTEM_GLEW=ON \
    -DWITH_SYSTEM_OPENJPEG=ON \
    -DWITH_INSTALL_PORTABLE=OFF \
    -DWITH_OPENCOLLADA=ON \
    -DWITH_GAMEENGINE=ON \
    -DWITH_RAYOPTIMIZATION=ON \
    -DWITH_JACK=OFF \
    -DWITH_PLAYER=ON \
    -DWITH_CODEC_FFMPEG=ON \
    -DWITH_CODEC_SNDFILE=ON \
    -DWITH_CYCLES=ON \
    -DWITH_CYCLES_CUDA_BINARIES=OFF \
    -DWITH_CYCLES_OSL=ON \
    -DWITH_FFTW3=ON \
    -DWITH_MOD_OCEANSIM=ON \
    -DPYTHON_VERSION=${_pyver} \
    -DPYTHON_LIBPATH=${MINGW_PREFIX}/lib \
    -DPYTHON_LIBRARY=python${_pyver}m \
    -DPYTHON_INCLUDE_DIRS=${MINGW_PREFIX}/include/python${_pyver}m \
    -DWITH_PYTHON_INSTALL=OFF \
    -DWITH_PYTHON_INSTALL_NUMPY=OFF \
    -DPYTHON_NUMPY_PATH=${MINGW_PREFIX}/lib/python${_pyver}/site-packages \
    -DCYCLES_OSL=${MINGW_PREFIX} \
    -DWITH_IMAGE_REDCODE=ON \
    -DWITH_OPENMP=ON \
    -DWITH_LLVM=ON \
    -DLLVM_STATIC=ON \
    ../${_realname}

  make
}

package() {
  cd "$srcdir/build-${MINGW_CHOST}"
  make install

  mkdir -p ${pkgdir}${MINGW_PREFIX}/lib
  cp -r bin ${pkgdir}${MINGW_PREFIX}/
  rm ${pkgdir}${MINGW_PREFIX}/bin/msgfmt.exe
  mv ${pkgdir}${MINGW_PREFIX}/bin/*.a ${pkgdir}${MINGW_PREFIX}/lib/

  ${MINGW_PREFIX}/bin/python3 -m compileall "${pkgdir}${MINGW_PREFIX}/share/blender/$blender_version"
  ${MINGW_PREFIX}/bin/python3 -m compileall \
  ${pkgdir}${MINGW_PREFIX}/share/blender/$blender_version/scripts/startup \
  ${pkgdir}${MINGW_PREFIX}/share/blender/$blender_version/scripts/modules \
  ${pkgdir}${MINGW_PREFIX}/share/blender/$blender_version/scripts/addons
}
